package demo2;

import demo1.Book;
import demo1.BookList;

import java.util.Scanner;

public class DelOperation implements IOoperation{
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书的名字：");
        String name = scanner.nextLine();
        int currentSize = bookList.getUseSize();
        int index = -1;

        for (int i = 0; i < bookList.getUseSize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                index = i;
                break;
            }
            if(i==currentSize){
                System.out.println("没有找到该图书");
                return;
            }

        }

        for (int i = index; i < currentSize-1; i++) {
            Book book = bookList.getBook(i+1);

            bookList.setBook(i, book);
        }
bookList.setBook(currentSize-1,null);
        bookList.setUseSize(currentSize-1);
        System.out.println("删除成功");
    }
}
